package cn.krui.project.controller.simpleshopsetting;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.project.controller.simpleshopsettingBaseController;
import cn.krui.project.bean.simpleshopsetting.category;
import cn.krui.project.bean.simpleshopsetting.category_view;
import cn.krui.project.service.simpleshopsetting.categoryService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.ObjectError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.stream.Collectors;
/**
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/4/8 16:37:58
 */
@Api(tags = "category", description = "商品类型")
@RestController
@RequestMapping("/simpleshopsetting/category")
public class categoryController extends simpleshopsettingBaseController {

    @Autowired
    private categoryService _categoryService;

    @PostMapping(value = "/add")
	@ApiOperation(value = "商品类型添加", httpMethod = "POST")
    public AppResult<Integer> add(@Valid category model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //主键不用判断
                if(columnname.equals("id"))continue;
                app.NO(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = _categoryService.add(model);
        if(i>0) i = model.getId();
        app.setDataSingle(i);
        app.OK("添加成功");
        return app;
    }

    @PostMapping(value = "/update")
	@ApiOperation(value = "商品类型修改", httpMethod = "POST")
    public AppResult<Integer> update(@Valid category model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                app.NO(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = _categoryService.update(model);
        app.OK("修改成功");
        app.setDataSingle(i);
        return app;
    }

    @PostMapping(value = "/del")
	@ApiOperation(value = "商品类型删除", httpMethod = "POST")
    public AppResult<Integer> del(Integer id){
        AppResult<Integer> app = new AppResult<Integer>();
        Integer i = _categoryService.del(id);
        app.OK("删除成功");
        app.setDataSingle(i);
        return app;
    }

    @GetMapping(value = "/info")
	@ApiOperation(value = "商品类型详情", httpMethod = "GET")
    public AppResult<category_view> info(Integer id){
        AppResult<category_view> app = new AppResult<category_view>();
        category_view i = _categoryService.info(id);
        app.setDataSingle(i);
        app.OK("查询成功");
        return app;
    }

    @PostMapping(value = "/select")
	@ApiOperation(value = "商品类型列表", httpMethod = "POST")
    public AppResult<category_view> select(
        ){
        AppResult<category_view> app = new AppResult<category_view>();
        QueryWrapper<category_view> where = new QueryWrapper<category_view>();
        List<category_view> i = _categoryService.select(where);
        DataArray<category_view> da=new DataArray<category_view>();
        da.setDataList(i);
        app.setDataArray(da);
        app.OK("查询成功");
        return app;
    }

    @PostMapping(value = "/selectpage")
    @ApiOperation(value = "商品类型列表分页", httpMethod = "POST")
    public AppResult<category_view> selectpage(
            @ApiParam(value = "页", required = true) @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "条", required = true) @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        AppResult<category_view> app = new AppResult<category_view>();
        QueryWrapper<category_view> where = new QueryWrapper<category_view>();
        IPage<category_view> pg = new Page<category_view>();
        pg.setCurrent(page); 
        pg.setSize(size);
        IPage<category_view> i = _categoryService.select(pg,where);
        int total = _categoryService.selectCount(where);
        DataArray<category_view> da = new DataArray<category_view>();
        da.setPageCount((total/size)+1);
        da.setDataCount(total);
        da.setPageIndex(page);
        da.setPageSize(size);
        da.setDataList(i.getRecords());
        app.setDataArray(da);
        app.OK("查询成功");
        return app;
    }
    @GetMapping(value = "/selecttree")
    @ApiOperation(value = "商品类型树形列表", httpMethod = "GET")
    public AppResult<category_view> selecttree(category_view model) {
        AppResult<category_view> app = new AppResult<category_view>();
        QueryWrapper<category_view> where = new QueryWrapper<category_view>();
        List<category_view> i = _categoryService.select(where);
        List<category_view> root = new ArrayList<category_view>();
        selectson(i,root,0);
        app.OK("查询成功");
        DataArray<category_view> dataArray =new DataArray<category_view>();
        dataArray.setDataList(root);
        app.setDataArray(dataArray);
        return app;
    }

    /**
     * 递归查询
     */
    private void selectson(List<category_view> all,List<category_view> son, Integer pid){
        List<category_view> select = all.stream().filter(s->s.getParent_id().equals(pid)).collect(Collectors.toList());
        if(select.size()==0) return;
        for (category_view _sitem : select) {
            List<category_view> children = new ArrayList<category_view>();
            selectson(all,children,_sitem.getId());
            if(children.size()>0){
                _sitem.setChildren(children);
            }
            son.add(_sitem);
        }
    }

}